package pc.nick.servlet;

import pc.nick.http.HttpServlet;
import pc.nick.http.HttpServletRequest;
import pc.nick.http.HttpServletResponse;
import pc.nick.http.HttpSession;

import java.io.PrintWriter;

/**
 * 登录servlet
 * 这是具体的 登录servlet的业务，子类实现了父类servlet的doGet、doPost方法
 */
public class LoginServlet extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest request,
                         HttpServletResponse response) {
        doPost(request,response);
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println(username + "\t" + password);

        System.out.println(request.getCookies());

        HttpSession session = request.getSession();
        session.setAttribute("username", username);
        session.setAttribute("password", password);
        PrintWriter out = response.getWriter();
        String jsessionid = request.getJsessionid();
        if (jsessionid != null){
            out.print("HTTP/1.1 200 OK\r\nSet-Cookie: JSESSIONID=" + jsessionid + "\r\n\r\n");
        }else{
            out.print("HTTP/1.1 200 OK\r\n\r\n");
        }
        out.print("<html>");
        out.print("<body>");
        out.print("login success! <br/>");
        out.print("sessionid: " + jsessionid + "<br/>");
        out.print("</body>");
        out.print("</html>");
        out.flush();
        out.close();
    }

}
